package com.mes.cloud.service.productpricegroup;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mes.cloud.api.dto.request.productpricegroup.*;
import com.mes.cloud.api.dto.response.productpricegroup.ProductPriceGroupProductProductDto;
import com.mes.cloud.exception.BaseException;
import com.mes.cloud.common.CommPager;
import com.mes.cloud.common.JsonResult;
import com.mes.cloud.dao.ProductPriceGroupDao;
import com.mes.cloud.dao.ProductPriceGroupProductDao;
import com.mes.cloud.entity.ProductPriceGroupProduct;
import com.mes.cloud.entity.vo.productpricegroup.ProductPriceGroupProductProductVo;
import com.mes.cloud.enums.HttpStatusEnum;
import com.mes.cloud.service.IPKeyGenerator;
import com.mes.cloud.util.DateUtil;
import com.mes.cloud.util.UserUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 产品价格组-产品关联
 *
 * @author wxm
 * @version 2019-09-24 14:49:07
 * @email xiaomin.wu1@pactera.com
 */
@Service
public class ProductPriceGroupProductService {

    private ProductPriceGroupProductDao productPriceGroupProductDao;

    private ProductPriceGroupDao productPriceGroupDao;

    private IPKeyGenerator iPKeyGenerator;

    @Autowired
    public ProductPriceGroupProductService(ProductPriceGroupProductDao productPriceGroupProductDao, ProductPriceGroupDao productPriceGroupDao, IPKeyGenerator iPKeyGenerator) {
        this.productPriceGroupProductDao = productPriceGroupProductDao;
        this.productPriceGroupDao = productPriceGroupDao;
        this.iPKeyGenerator = iPKeyGenerator;
    }

    /**
     * 分页查列表
     *
     * @param productPriceGroupProductPageListRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<ProductPriceGroupProductProductDto>> pageList(ProductPriceGroupProductPageListRequest productPriceGroupProductPageListRequest) {
        Page<ProductPriceGroupProductProductDto> page = PageHelper.startPage(productPriceGroupProductPageListRequest.getCurrentPage(), productPriceGroupProductPageListRequest.getPageSize());
        //查询条件
        ProductPriceGroupProductListRequest selectParam = new ProductPriceGroupProductListRequest();
        BeanUtils.copyProperties(productPriceGroupProductPageListRequest, selectParam);
        List<ProductPriceGroupProductProductVo> productPriceGroupProductProductVoList = productPriceGroupProductDao.list(selectParam);
        List<ProductPriceGroupProductProductDto> productPriceGroupProductProductDtoList = new ArrayList<>();
        for (ProductPriceGroupProductProductVo productPriceGroupProductProductVo : productPriceGroupProductProductVoList) {
            ProductPriceGroupProductProductDto productPriceGroupProductProductDto = new ProductPriceGroupProductProductDto();
            BeanUtils.copyProperties(productPriceGroupProductProductVo, productPriceGroupProductProductDto);
            productPriceGroupProductProductDtoList.add(productPriceGroupProductProductDto);
        }
        CommPager<ProductPriceGroupProductProductDto> pageResult = new CommPager<>(page, productPriceGroupProductProductDtoList);
        JsonResult<CommPager<ProductPriceGroupProductProductDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 绑定产品
     *
     * @param productPriceGroupProductBindProductRequest
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public JsonResult bindProduct(ProductPriceGroupProductBindProductRequest productPriceGroupProductBindProductRequest) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
        jsonResult.setCode(HttpStatusEnum.OK.getCode());
        if (CollectionUtils.isEmpty(productPriceGroupProductBindProductRequest.getBindProductRequestList())) {
            return jsonResult;
        }
        for (BindProductRequest bindProductRequest : productPriceGroupProductBindProductRequest.getBindProductRequestList()) {
            //有效开始时间
            Date startDate = bindProductRequest.getEffectiveStartDate();
            if (startDate == null) {
                startDate = new Date();
            }
            //有效结束时间
            Date endDate = bindProductRequest.getEffectiveEndDate();
            if (endDate == null) {
                endDate = DateUtil.formatDate("2099-01-01 00:00:00");
            }
            //开始日期比结束日期晚
            if (startDate.getTime() > endDate.getTime()) {
                continue;
            }
            //产品ID
            String productId = bindProductRequest.getProductId();
            if (StringUtils.isEmpty(productId)) {
                continue;
            }
            //产品价格组ID
            String productPriceGroupId = productPriceGroupProductBindProductRequest.getProductPriceGroupId();
            //判断添加的产品时间区间是否与已存在的有交集
            int count = productPriceGroupProductDao.selectConflictCount(startDate, endDate, productId, productPriceGroupId);
            //区间有交集
            if (count > 0) {
                jsonResult.setCode(HttpStatusEnum.DURATION_CONFLICT.getCode()).setMsg(HttpStatusEnum.DURATION_CONFLICT.getMessage());
                return jsonResult;
            }
            //直接插入
            ProductPriceGroupProduct productPriceGroupProductInsert = new ProductPriceGroupProduct();
            productPriceGroupProductInsert.setProductPriceGroupId(productPriceGroupProductBindProductRequest.getProductPriceGroupId());
            productPriceGroupProductInsert.setProductId(bindProductRequest.getProductId());
            productPriceGroupProductInsert.setStandardPrice(bindProductRequest.getStandardPrice());
            productPriceGroupProductInsert.setStandardPurchasePrice(bindProductRequest.getStandardPurchasePrice());
            productPriceGroupProductInsert.setStandardSellPrice(bindProductRequest.getStandardSellPrice());
            productPriceGroupProductInsert.setEffectiveEndDate(endDate);
            productPriceGroupProductInsert.setEffectiveStartDate(startDate);
            productPriceGroupProductInsert.setId(iPKeyGenerator.getGenerateKey());
            productPriceGroupProductInsert.setUpdateDateStr(DateUtil.updateDateStr());
            productPriceGroupProductInsert.setCreateBy(UserUtil.getCreateByName());
            productPriceGroupProductInsert.setUpdateBy(UserUtil.getUpdateByName());
            productPriceGroupProductInsert.setCreateByNo(UserUtil.getCreateByNo());
            productPriceGroupProductInsert.setUpdateByNo(UserUtil.getUpdateByNo());
            productPriceGroupProductDao.insertSelective(productPriceGroupProductInsert);
        }

        return jsonResult;
    }

    /**
     * 解绑产品
     * 更新产品价格组的产品数
     *
     * @param productPriceGroupProductBatchDeleteRequest
     * @return
     */
    public JsonResult batchDelete(ProductPriceGroupProductBatchDeleteRequest productPriceGroupProductBatchDeleteRequest) {
        JsonResult jsonResult = new JsonResult();
        //批量删除
        productPriceGroupProductDao.batchDelete(productPriceGroupProductBatchDeleteRequest.getIds(),
                DateUtil.updateDateStr(), UserUtil.getEmployeeNo(), UserUtil.getName());
        return jsonResult;
    }

    /**
     * 产品定价查询
     *
     * @param pageRequest
     * @return JsonResult
     */
    public JsonResult<CommPager<ProductPriceGroupProductProductDto>> findPriceGroupByProductId(ProductPriceGroupProductPageRequest pageRequest) {
        Page<ProductPriceGroupProductProductDto> page = PageHelper.startPage(pageRequest.getCurrentPage(), pageRequest.getPageSize());
        //查询条件
        List<ProductPriceGroupProductProductVo> productVoList = productPriceGroupProductDao.findPriceGroupByProductId(pageRequest.getProductId());
        List<ProductPriceGroupProductProductDto> productDtoList = new ArrayList<>();
        for (ProductPriceGroupProductProductVo productVo : productVoList) {
            ProductPriceGroupProductProductDto productDto = new ProductPriceGroupProductProductDto();
            BeanUtils.copyProperties(productVo, productDto);
            productDtoList.add(productDto);
        }
        CommPager<ProductPriceGroupProductProductDto> pageResult = new CommPager<>(page, productDtoList);
        JsonResult<CommPager<ProductPriceGroupProductProductDto>> jsonResult = new JsonResult<>();
        jsonResult.setMsg(HttpStatusEnum.OK.getMessage()).setCode(HttpStatusEnum.OK.getCode()).setData(pageResult);
        return jsonResult;
    }

    /**
     * 更新价格组绑定产品中间表信息
     *
     * @param productPriceGroupProductUpdateRequest
     * @return
     */
    public JsonResult update(ProductPriceGroupProductUpdateRequest productPriceGroupProductUpdateRequest) {
        ProductPriceGroupProduct productPriceGroupProduct = new ProductPriceGroupProduct();
        BeanUtils.copyProperties(productPriceGroupProductUpdateRequest, productPriceGroupProduct);
        productPriceGroupProduct.setUpdateDateStr(DateUtil.updateDateStr());
        productPriceGroupProduct.setUpdateBy(UserUtil.getUpdateByName());
        productPriceGroupProduct.setUpdateByNo(UserUtil.getUpdateByNo());
        int count = productPriceGroupProductDao.updateByPrimaryKeySelective(productPriceGroupProduct);
        JsonResult jsonResult = new JsonResult();
        if (count == 1) {
            jsonResult.setMsg(HttpStatusEnum.OK.getMessage());
            jsonResult.setCode(HttpStatusEnum.OK.getCode());
        } else {
            throw new BaseException(HttpStatusEnum.FAIL.getCode(),HttpStatusEnum.FAIL.getMessage());
        }
        return jsonResult;
    }
}
